package com.mornd.gulimall.cart.entity;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: mornd
 * @dateTime: 2023/8/20 - 6:58
 * 购物车
 */

public class Cart {
    private List<CartItem> items;

    // 商品数量
    private Integer countNum;

    // 商品类型数量
    private Integer countType;

    // 商品总价
    private BigDecimal totalAmount;

    // 价格减免
    private BigDecimal reduce = BigDecimal.ZERO;

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        if (!CollectionUtils.isEmpty(items)) {
            return this.items.stream()
                    .map(CartItem::getCount)
                    .reduce(0, Integer::sum);
        } else {
            return 0;
        }
    }

    public Integer getCountType() {
        return CollectionUtils.isEmpty(items) ? 0 : items.size();
    }

    public BigDecimal getTotalAmount() {
        if (!CollectionUtils.isEmpty(items)) {
            // 购物车总价
            totalAmount = this.items.stream()
                    //.filter(CartItem::getCheck)
                    .map(i -> i.getPrice().multiply(new BigDecimal(i.getCount().toString())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            // 减去优惠
            totalAmount = totalAmount.subtract(reduce);
        } else {
            totalAmount = BigDecimal.ZERO;
        }
        return totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
